/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUITest.java
 *
 * Created on 26/10/2009, 11:42:36 AM
 */

package Test;

import Drawing.TankImage;
import Game.Match;
import Game.Objects.Rock;
import Game.Shot;
import Game.Tank;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;

/**
 *
 * @author Andrew
 */
public class GUITest extends javax.swing.JPanel {

    /** Creates new form GUITest */

    Tank tank;
    TankImage img;
    ArrayList<Rock> rocks;


    private static final int width = Match.BOUNDS_WIDTH;
    private static final int height = Match.BOUNDS_HEIGHT;

    public GUITest() {
        
        initComponents();
        tank = new Tank("lol", 200, 200);
        img = new TankImage(tank);
        this.setBackground(Color.BLACK);

        setFocusable(true);
        addKeyListener(new KeyboardListener());
        //this.setSize(width, height);
        initRocks();
    }

    private void initRocks()
    {
        rocks = new ArrayList<Rock>();

        Random rangen = new Random();
        int amtRocks = rangen.nextInt(14) + 3;


        for(int i=0; i<amtRocks; i++)
        {
            int xPos = rangen.nextInt(width);
            int yPos = rangen.nextInt(height);
            rocks.add(new Rock(xPos, yPos));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setPreferredSize(new Dimension(width, height));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    @Override
    public void paint(Graphics g)
    {
        super.paintComponent(g);

        for(int i=0; i<rocks.size(); i++)
        {
            rocks.get(i).draw(g);
        }
        
        img.draw(g);
        g.setColor(Color.WHITE);
        g.drawString(Match.VERSION_NO, 10, height-10);
    }



    public static void main(String[] args)
    {
        JFrame frame = new JFrame("GUI Test");
        GUITest test = new GUITest();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(test);
        frame.pack();

        //sets to middle of screen and displays it
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenDimension = tk.getScreenSize();
        Dimension frameDimension = frame.getSize();
        frame.setLocation((screenDimension.width-frameDimension.width)/2,(screenDimension.height-frameDimension.height)/2);
        frame.setVisible(true);
    }


    private boolean safeMove(int[] xPoints, int[] yPoints)
    {

        for(int r=0; r<rocks.size(); r++)
        {
            Rock rock = rocks.get(r);
            for(int i=0; i<xPoints.length; i++)
            {
                if(rock.contains(xPoints[0], yPoints[0]))
                {
                    return false;
                }
            }
        }
        return true;
    }

    


    private class KeyboardListener implements KeyListener
    {

        public void keyTyped(KeyEvent e)
        {
            //does nothing
        }

        public void keyPressed(KeyEvent e)
        {
            int key = e.getKeyCode();

            

            boolean canMove = true;

            if(canMove)
            {
                if(key == KeyEvent.VK_UP || key == KeyEvent.VK_W) //this is move fowards
                {
                    tank.moveForwards();
                    if(!safeMove(img.getPointsX(), img.getPointsY()))
                    {   tank.moveBackwards();
                    }
                }
                if(key == KeyEvent.VK_A || key == KeyEvent.VK_LEFT) //this is move left
                {
                    tank.rotateLeft();
                    if(!safeMove(img.getPointsX(), img.getPointsY()))
                    {   tank.rotateRight();
                    }
                }
                if(key == KeyEvent.VK_D || key == KeyEvent.VK_RIGHT)
                {
                    tank.rotateRight();
                    if(!safeMove(img.getPointsX(), img.getPointsY()))
                    {   tank.rotateLeft();
                    }
                }
                if(key == KeyEvent.VK_S || key == KeyEvent.VK_DOWN)
                {
                    tank.moveBackwards();
                    if(!safeMove(img.getPointsX(), img.getPointsY()))
                    {   tank.moveForwards();
                    }
                }
                if(key == KeyEvent.VK_SPACE)
                {
                    tank.shoot();
                    tank.getShot().addActionListener(new ShotListener());
                }
                repaint();
            }
        }

        public void keyReleased(KeyEvent e)
        {
            //does nothing
        }

    }



    private class ShotListener implements ActionListener
    {

        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() instanceof Shot)
            {
                if(!e.getActionCommand().equalsIgnoreCase(Shot.HIT_OBSTACLE_MESSAGE))
                {
                    Shot shot = (Shot) e.getSource();
                    int[] xs = {shot.getX() };
                    int[] ys = {shot.getY() };
                    if(!safeMove(xs, ys)) //if it hits a rock
                    {
                        shot.hitObstacle();
                    }
                }
                

            }
            repaint();
        }

    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
